package com.banzhi.shop.entity.InviteShareSuCaimatterInfo;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.banzhi.shop.R;
import com.banzhi.shop.activity.ViewPagerActivity;
import com.banzhi.shop.adapter.BaseAdapter;
import com.banzhi.shop.adapter.CommonAdapter;
import com.banzhi.shop.utils.ASUtils;
import com.banzhi.shop.utils.BitmapUtils;
import com.banzhi.shop.utils.ShareUtils;
import com.banzhi.shop.utils.ToastUtils;
import com.banzhi.shop.view.SpacingTextView;
import com.squareup.picasso.Picasso;

import java.io.Serializable;
import java.util.List;

/**
 * Created by JIANGFEI on 2018/4/20.
 */

public class InviteShareSuCaiAdapter extends BaseAdapter<InviteShareSuCai_Data> {
    private Context context;

    public InviteShareSuCaiAdapter(Context context, List<InviteShareSuCai_Data> data) {
        super( context, data );
        this.context = context;

    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
       final InviteShareSuCai_Data data = getData().get( position );
        viewHolder holder = null;
        LinearLayoutManager layoutManager = null;
        if (convertView == null) {
            holder = new viewHolder();
            convertView = getInflater().inflate( R.layout.item_invite_share_sucai,null );
            holder.tvtitle3 =(SpacingTextView)convertView.findViewById( R.id.tv_title3 );
            holder.tvtitle3.setLetterSpacing(1);
            holder.rvimage3 = (RecyclerView) convertView.findViewById(R.id.rv_image3);
            holder.llbaochun3 = (LinearLayout) convertView.findViewById(R.id.ll_baocun3);
            holder.llcopy3 = (LinearLayout) convertView.findViewById(R.id.ll_copy3);
            holder.llshare3 = (LinearLayout) convertView.findViewById(R.id.ll_share3);
            holder.ivimage3 = (ImageView) convertView.findViewById(R.id.iv_image3);
            layoutManager = new LinearLayoutManager(getContext());
            layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
            holder.rvimage3.setLayoutManager(layoutManager);
            convertView.setTag(holder);

        }
            holder = (InviteShareSuCaiAdapter.viewHolder) convertView.getTag();
           // holder.tvtitle3.setText(data.getTitle());
        //把换行加回车效果改成只换行
        holder.tvtitle3.setText(data.getDetail().replace("\r\n","\n"));
        if (ASUtils.isCanUse(data.getThumb())) {
            Picasso.with(getContext()).load(data.getThumb())
                    .placeholder( R.mipmap.zhanweitu).into(holder.ivimage3);
        }
        CommonAdapter<String> adapter = new CommonAdapter<String>(context,
                R.layout.item_invite_share_sucai_recyclerview,data.getThumb_url()) {
            @Override
            public void convert(ViewHolder holder, String s, int position) {
                holder.setImageURI(R.id.iv_image3, s);
            }
        };
        holder.rvimage3.setAdapter(adapter);
        adapter.setOnItemClickListener(new CommonAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                Intent intent = new Intent(getContext(), ViewPagerActivity.class);
                intent.putExtra("images", (Serializable)data.getThumb_url());
                intent.putExtra("position", position);
                getContext().startActivity(intent);

            }
        });
        holder.llbaochun3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            //  showPopupwindow(data.getThumb_url(),data.getThumb());

                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        for (int i = 0; i < data.getThumb_url().size(); i++) {
                            BitmapUtils.saveImageToGallery((Activity) getContext(), BitmapUtils.returnBitmap(data.getThumb_url().get(i)));
                        }
                        if (ASUtils.isCanUse(data.getThumb())) {
                            BitmapUtils.saveImageToGallery((Activity) getContext(), BitmapUtils.returnBitmap(data.getThumb()));
                        }
                    }
                }).start();

               int sum = data.getThumb_url().size()+1;
               ToastUtils.makeToastShort("保存成功"+ sum +"张");




            }
        });
        holder.llcopy3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ShareUtils.copyLianjie((Activity) getContext(),data.getDetail());
            }
        });
        holder.llshare3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //daixie
                inviteShareCallBack.inviteShare(data,position);

            }
        });
        return convertView;
    }
    class viewHolder{
        SpacingTextView tvtitle3;
        RecyclerView rvimage3;
        LinearLayout llbaochun3;
        LinearLayout llcopy3;
        LinearLayout llshare3;
        ImageView ivimage3;

    }
    private void showPopupwindow(final List<String> images, final String thumb) {
        View contentView = LayoutInflater.from(getContext()).inflate(R.layout.popupwindow_baochun_image, null);

        int width = getContext().getResources().getDisplayMetrics().widthPixels;
        int height = getContext().getResources().getDisplayMetrics().heightPixels;

        final PopupWindow popWindow = new PopupWindow(contentView, width, height);
        popWindow.setAnimationStyle(R.style.AnimBottom);

        popWindow.setFocusable(true);
        popWindow.setOutsideTouchable(false);// 设置允许在外点击消失
        //设置各个控件的点击响应
        TextView tvcancle = (TextView) contentView.findViewById(R.id.tv_cancle);
        TextView tvtrue = (TextView) contentView.findViewById(R.id.tv_true);
        RelativeLayout rl = (RelativeLayout) contentView.findViewById(R.id.rl_background);
        LinearLayout ll = (LinearLayout) contentView.findViewById(R.id.ll);
        rl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popWindow.dismiss();
            }
        });
        ll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
        tvcancle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popWindow.dismiss();
            }
        });
        tvtrue.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        for (int i = 0; i < images.size(); i++) {
                            BitmapUtils.saveImageToGallery((Activity) getContext(), BitmapUtils.returnBitmap(images.get(i)));
                        }
                        if (ASUtils.isCanUse(thumb)) {
                            BitmapUtils.saveImageToGallery((Activity) getContext(), BitmapUtils.returnBitmap(thumb));
                        }
                    }
                }).start();
                popWindow.dismiss();
               // ToastUtils.makeToastShort("保存图片成功.");
                ToastUtils.makeToastShort("保存成功"+images.size()+"张");
            }
        });
        popWindow.showAtLocation(contentView, Gravity.CENTER | Gravity.CENTER_HORIZONTAL, 0, 0);
    }


    public interface InviteShareCallBack{
        void inviteShare(InviteShareSuCai_Data data, int position);

    }
    private InviteShareCallBack inviteShareCallBack;

    public  void setInviteShareCallBack(InviteShareCallBack callBack){
        this.inviteShareCallBack = callBack;
    }
}
